/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.common.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

@Configuration
public class RabbitMQConfig implements RabbitListenerConfigurer {

	@Override
	public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
		registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
	}

	// 1、创建Exchange（operationLogTopicExchange）
	@Bean("operationLogTopicExchange")
	public TopicExchange createOperationLogTopicExchange() {
		return new TopicExchange("operationLogTopicExchange", true, false);
	}

	// 2、创建Queue（operationLogQueue）
	@Bean("operationLogQueue")
	public Queue createOperationLogQueue() {
		return new Queue("operationLogQueue", true, false, false, null);
	}

	// 3、创建绑定（binding1）
	@Bean("binding1")
	public Binding binding1(@Qualifier("operationLogTopicExchange") TopicExchange topicExchange,
			@Qualifier("operationLogQueue") Queue queue) {
		return BindingBuilder.bind(queue).to(topicExchange).with("operationLogRoutingKey");
	}

	// 1、创建exchange（exceptionLogTopicExchange）
	@Bean("exceptionLogTopicExchange")
	public TopicExchange createExceptionLogTopicExchange() {
		return new TopicExchange("exceptionLogTopicExchange", true, false);
	}

	// 2、创建queue（exceptionLogQueue）
	@Bean("exceptionLogQueue")
	public Queue createExceptionLogQueue() {
		return new Queue("exceptionLogQueue", true, false, false, null);
	}

	// 3、创建绑定（binding2）
	@Bean("binding2")
	public Binding binding2(@Qualifier("exceptionLogTopicExchange") TopicExchange topicExchange,
			@Qualifier("exceptionLogQueue") Queue queue) {
		return BindingBuilder.bind(queue).to(topicExchange).with("exceptionLogRoutingKey");
	}

	@Bean
	public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
		return new Jackson2JsonMessageConverter();
	}

	@Bean
	public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
		final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
		return rabbitTemplate;
	}

	@Bean
	MessageHandlerMethodFactory messageHandlerMethodFactory() {
		DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
		messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
		return messageHandlerMethodFactory;
	}

	@Bean
	public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
		return new MappingJackson2MessageConverter();
	}

}
